package com.example.weddingapp.activity;

import androidx.lifecycle.AndroidViewModel;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.base.BaseObserver;
import com.example.weddingapp.databinding.ActivityWalletBinding;
import com.example.weddingapp.utils.CommonUtil;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import cn.leancloud.AVUser;

public class WalletActivity extends BaseActivity<AndroidViewModel, ActivityWalletBinding> implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        setNoTitle();
        showContentView();

        bindingView.ibBack.setOnClickListener(this);
        bindingView.tvValueAdd.setOnClickListener(this);
    }


    @Override
    protected void initData() {
        super.initData();
        AVUser user = AVUser.getCurrentUser();
        //查询余额
        AVQuery<AVObject> query = new AVQuery<>("Account");
        query.whereEqualTo("user", user);
        query.getFirstInBackground().subscribe(new BaseObserver<AVObject>() {
            @Override
            public void onNext(AVObject avObject) {
                super.onNext(avObject);
                int anInt = avObject.getInt("amount");
                double d = Double.valueOf(anInt) / 100;
                String money = CommonUtil.formatAmount(d);
                bindingView.tvAmount.setText(money);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back:
                //返回
                finish();
                break;
            case R.id.tv_value_add:
                //充值
                startActivity(new Intent(this, ValueAddActivity.class));
                break;
        }
    }
}
